package lightswitch.server.code.model;

import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

import lightswitch.server.code.gui.MainFrame;
import lightswitch.server.code.object.Database;
import lightswitch.server.code.object.Reference;
import lightswitch.server.code.object.Table;

public class MyTableModel extends AbstractTableModel {
    public Vector<String> columnNames;
    public Vector<Vector<Object>> dataVector;
    
    public MyTableModel(Vector<String> cn, Vector<Vector<Object>> dv) {
        super();
        dataVector = dv;
        columnNames = cn;
    }
    
    public void resetTableModel(Vector<String> cn, Vector<Vector<Object>> dv) {
        dataVector = dv;
        columnNames = cn;
    }
    
    @Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return columnNames.size();
    }

    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return dataVector.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        int maxRow = dataVector.size();
        if (rowIndex<0 || rowIndex>=maxRow) return null;
        int maxCol = dataVector.get(0).size();
        if (columnIndex<0 || columnIndex>=maxCol) return null;
       return dataVector.get(rowIndex).get(columnIndex);
    }
    
    public String getColumnName(int col) {
        return columnNames.get(col);
    }
    
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public boolean isCellEditable(int row, int col) {
        return true;
    }
    
    public void setValueAt(Object value, int row, int col) {
        Vector<Object> rowVector = dataVector.get(row);
        if(MainFrame.isShowTableData.equals(Boolean.FALSE) && (col ==3 || col==4)){
            rowVector.set(col, value);
            dataVector.set(row, rowVector);
            fireTableCellUpdated(row, col);
            MainFrame.currentDatabase.flagChangeData = true;
            return;
        }
        
        if(MainFrame.isShowTableData.equals(Boolean.FALSE) && (col==1||col==2)){
            Table tb = Database.getLoadedTable(Database.lastChosenTable);
            for (Reference r : tb.getListOfReferences()){
                if (r.getField().equals(rowVector.get(0)))
                {
                    JOptionPane.showMessageDialog(null, "Cannot edit with your value.\nCheck your value, reference or primary key.", "Message", 1);
                    return;
                }
            }
            
            for (Reference r : Database.getRefToMe(Database.lastChosenTable)){
                if (r.getFieldRef().equals(rowVector.get(0))){
                    JOptionPane.showMessageDialog(null, "Cannot edit with your value.\nCheck your value, reference or primary key.", "Message", 1);
                    return;
                }
            }
            
            rowVector.set(col, value);
            dataVector.set(row, rowVector);
            fireTableCellUpdated(row, col);
            MainFrame.currentDatabase.flagChangeData = true;
            return;
            
        }
        
        if (Database.checkEditDataDirectInCell(rowVector, col, value)){
            rowVector.set(col, value);
            dataVector.set(row, rowVector);
            fireTableCellUpdated(row, col);
            MainFrame.currentDatabase.flagChangeData = true;
            return;
        }
        
        JOptionPane.showMessageDialog(null, "Cannot edit with your value.\nCheck your value, reference or primary key.", "Message", 1);
    }
}
